package com.example.piedrapapeltijeralagartospock;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class Inicio extends Activity {
	RelativeLayout imgPiedra, imgPapel, imgTijeras, imgLagarto, imgSpock, botones, logo;
	public static PuntuacionesInternas puntuacionesFich;
	public static PuntuacionesXML puntuacionesXml;
	public static PuntuacionesSQL puntuacionesSQL;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_inicio);
		
		puntuacionesFich= new PuntuacionesInternas(this);
		puntuacionesXml= new PuntuacionesXML(this);
		puntuacionesSQL = new PuntuacionesSQL (this);
		
		MyListener l= new MyListener(this);
		ImageView unPLayer = (ImageView)findViewById(R.id.but_1jugador);
		unPLayer.setOnClickListener(l);
		ImageView dosPLayer = (ImageView)findViewById(R.id.but_2jugadores);
		dosPLayer.setOnClickListener(l);
		imgPiedra = (RelativeLayout)findViewById(R.id.layPiedra);
		imgPapel = (RelativeLayout)findViewById(R.id.layPapel);
		imgTijeras = (RelativeLayout)findViewById(R.id.layTijeras);
		imgLagarto = (RelativeLayout)findViewById(R.id.layLagarto);
		imgSpock = (RelativeLayout)findViewById(R.id.laySpock);
		botones = (RelativeLayout)findViewById(R.id.BotonesDeInicio);
		logo = (RelativeLayout)findViewById(R.id.layLogo);
		
		temporizador();
	}
	
	//METODOS PARA EL HILO NECESARIO PARA ANIMAR CON TIEMPOS Y PAUSAS
		//Creador del control de eventos
		final Handler miHandler= new Handler();
		
		//Ejecuciones para simular movimiento
		final Runnable piedra = new Runnable(){
			public void run(){
				animar(imgPiedra,800,0,0,0);
			};
		};
		final Runnable papel = new Runnable(){
			public void run(){
				animar(imgPapel,500,0,800,0);
			};
		};
		final Runnable tijeras = new Runnable(){
			public void run(){
				animar(imgTijeras,-800,0,500,0);
			};
		};
		final Runnable lagarto = new Runnable(){
			public void run(){
				animar(imgLagarto,-800,0,800,0);
			};
		};
		final Runnable spock = new Runnable(){
			public void run(){
				animar(imgSpock,800,0,800,0);
			};
		};
		final Runnable botonesAnimados = new Runnable(){
			public void run(){
				animar(botones,0,0,0,0);
			};
		};
		final Runnable logoAnimar = new Runnable(){
			public void run(){
				logo.setVisibility(View.VISIBLE);
			};
		};
		
		// Creacion del hilo
	protected void temporizador() {
		Thread t = new Thread() {
			public void run() {
				try {
					miHandler.post(piedra);
					Thread.sleep(1000);
					miHandler.post(papel);
					Thread.sleep(1000);
					miHandler.post(tijeras);
					Thread.sleep(1000);
					miHandler.post(lagarto);
					Thread.sleep(1000);
					miHandler.post(spock);
					Thread.sleep(1000);
					miHandler.post(logoAnimar);
					Thread.sleep(1000);
					miHandler.post(botonesAnimados);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		t.start();
	}
		//FIN DE LOS METODOS
	
	
	//Metodo generico para animar
	public void animar(RelativeLayout objetoAnimar, int xOrigen,int xFin,int yOrigen,int yFin){
		AnimationSet set = new AnimationSet(true);
		Animation animation = null;
		animation = new TranslateAnimation(xOrigen,xFin,yOrigen,yFin);
		animation.setDuration(500);
		set.addAnimation(animation);
		LayoutAnimationController controlador = new LayoutAnimationController(set, 0.25f);
		objetoAnimar.setVisibility(View.VISIBLE);
		objetoAnimar.setLayoutAnimation(controlador);
		objetoAnimar.startAnimation(animation);
	}
	
	public void iniciarJuego(int numJugadores){
		Intent intent = new Intent(Inicio.this, PedirNombres.class);
		intent.putExtra("ModoDeJuego", numJugadores);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
		 Intent intent = new Intent(this,MenuRecord.class);
	   try{
	    switch (item.getItemId()) {
	    case R.id.menuReglas:            
	        startActivity(new Intent(this,MenuReglas.class));
	        break;  
	    case R.id.menuReglasGraficas:            
	        startActivity(new Intent(this,MenuReglasGraficas.class));
	        break;
	    case R.id.menuReglasVideo:            
	    	startActivity(new Intent(this,MenuReglasVideo.class));
	        break;
	    case R.id.menuInformacion:            
	        startActivity(new Intent(this,MenuInformacion.class));
	        break;
	    case R.id.menu2ficherotxt:            
	        intent.putExtra("ModoCarga", 1);
	        startActivity(intent);
	        break;
	    case R.id.menu2ficherobin:    
	    	intent.putExtra("ModoCarga", 2);
	        startActivity(intent);
	        break;
	    case R.id.menu2ficheroxml:
	    	intent.putExtra("ModoCarga", 3);
	        startActivity(intent);
	        break;
	    case R.id.menu2basesql: 
	    	intent.putExtra("ModoCarga", 4);
	        startActivity(intent);
	        break;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	   }catch(Exception e){
	      Log.i("PiedraPapelMenu", e.getMessage(), e);
	   }
	   return true;
	}
}
